package com.exposit.books.domain;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;


@Entity
@Table(name = "books")
public class Book {
	
	private Long id;	
	private String name;	
	private Author author;
	private List<Genre> genre;
	private Long count;
	private Long purchasePrice;
	private Long retailPrice;
	private Long status;
	private List<Buyer> likedUsers;
	private List<Comment> comments;
	private List<Order> orders;
	
	
	public Book(){}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public Book(String name, Long count, Long purchasePrice, Long retailPrice)
	{
		this.name = name;
		this.count = count;
		this.purchasePrice = purchasePrice;
		this.retailPrice = retailPrice;
		status = 0L;
		likedUsers = new ArrayList();
		comments = new ArrayList();
		orders = new ArrayList();
	}
	
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name="book_id", unique=true, nullable=false)
	@PrimaryKeyJoinColumn
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@Column(nullable=false, name="name")
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@ManyToMany(fetch = FetchType.LAZY/*, cascade = CascadeType.ALL*/)
	@JoinTable(name = "book_author", 
			joinColumns = { 
				@JoinColumn(name = "book_id", nullable = false) }, 
				inverseJoinColumns = { @JoinColumn(name = "author_id", nullable = false) })
	@Cascade({CascadeType.SAVE_UPDATE, CascadeType.DELETE})
	public Author getAuthor() {
		return author;
	}

	public void setAuthors(Author author) {
		this.author = author;
	}

	@ManyToMany(fetch = FetchType.LAZY/*, cascade = CascadeType.ALL*/)
	@JoinTable(name = "book_genre", 
			joinColumns = { 
				@JoinColumn(name = "book_id", nullable = false) }, 
				inverseJoinColumns = { @JoinColumn(name = "genre_id", nullable = false) })
	@Cascade({CascadeType.SAVE_UPDATE, CascadeType.DELETE})
	public List<Genre> getGenre() {
		return genre;
	}

	public void setGenre(List<Genre> genre) {
		this.genre = genre;
	}

	@Column(nullable=false, name="count")
	public Long getCount() {
		return count;
	}

	public void setCount(Long count) {
		this.count = count;
	}

	@Column(nullable=false, name="purchasePrice")
	public Long getPurchasePrice() {
		return purchasePrice;
	}

	public void setPurchasePrice(Long purchasePrice) {
		this.purchasePrice = purchasePrice;
	}

	@Column(nullable=false, name="retailPrice")
	public Long getRetailPrice() {
		return retailPrice;
	}

	public void setRetailPrice(Long retailPrice) {
		this.retailPrice = retailPrice;
	}

	@Column(nullable=false, name="status")
	public Long getStatus() {
		return status;
	}

	public void setStatus(Long status) {
		this.status = status;
	}

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "book")
	@Cascade({CascadeType.SAVE_UPDATE, CascadeType.DELETE})
	public List<Comment> getComments() {
		return comments;
	}

	public void setComments(List<Comment> comments) {
		this.comments = comments;
	}

	@ManyToMany(fetch = FetchType.LAZY/*, cascade = javax.persistence.CascadeType.ALL*/)
	@JoinTable(
	        name = "book_author",
	        joinColumns = @JoinColumn(name = "author_id"),
	        inverseJoinColumns = @JoinColumn(name = "book_id")
	)
	@Cascade({CascadeType.SAVE_UPDATE, CascadeType.DELETE})
	public List<Buyer> getLikedUsers() {
		return likedUsers;
	}

	public void setLikedUsers(List<Buyer> likedUsers) {
		this.likedUsers = likedUsers;
	}

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "book")
	@Cascade({CascadeType.SAVE_UPDATE, CascadeType.DELETE})
	public List<Order> getOrders() {
		return orders;
	}

	public void setOrders(List<Order> orders) {
		this.orders = orders;
	}

	public Book clone()
	{
		Book book = new Book(name, count, purchasePrice, retailPrice);
		book.setAuthors(getAuthor());
		book.setComments(getComments());
		book.setGenre(getGenre());
		book.setLikedUsers(getLikedUsers());
		book.setOrders(getOrders());
		book.setStatus(getStatus());
		return book;
	}

}
